/**
 * 
 */
package org.apache.lucene.store.entity;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.SingleInstanceLockFactory;

import com.edocbase.entity.file.EntityFile;
import com.edocbase.entity.file.EntityFolder;

/**
 * @author lifei114@gmail.com
 *
 */
public class EntityDirectory extends Directory {
	private static String directory="lucenuestore";
	HashMap<String,EntityFile> fileMap = new HashMap<String,EntityFile>();
	
	public EntityDirectory() {
		super();
		//setLockFactory(NoLockFactory.getNoLockFactory());
		setLockFactory(new SingleInstanceLockFactory());
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#listAll()
	 */
	@Override
	public String[] listAll() throws IOException {
		EntityFolder folder = new EntityFolder(directory);
		List<EntityFile> fileList = folder.listFiles();
		String[] names = new String[fileList.size()];
		for (int i=0;i< fileList.size();i++){
			names[i] = fileList.get(i).getFilename();
		}
		return names;
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#fileExists(java.lang.String)
	 */
	@Override
	public boolean fileExists(String name) throws IOException {
		return getFile(name).exists();
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#fileModified(java.lang.String)
	 */
	@Override
	public long fileModified(String name) throws IOException {
		return getFile(name).getLastModified();
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#touchFile(java.lang.String)
	 */
	@Override
	public void touchFile(String name) throws IOException {
		EntityFile file = getFile(name);
		file.setLastModified(System.currentTimeMillis()+1);
		file.save();
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#deleteFile(java.lang.String)
	 */
	@Override
	public void deleteFile(String name) throws IOException {
		synchronized (fileMap) {
			getFile(name).delete();
			System.out.println("delete " + name);
			fileMap.remove(name);
		}
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#fileLength(java.lang.String)
	 */
	@Override
	public long fileLength(String name) throws IOException {
		return getFile(name).getLength();
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#createOutput(java.lang.String)
	 */
	@Override
	public IndexOutput createOutput(String name) throws IOException {
		EntityFile file = getFile(name);
		file.open();
		EntityOutput output = new EntityOutput(file);
		return output;
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#openInput(java.lang.String)
	 */
	@Override
	public IndexInput openInput(String name) throws IOException {
		EntityFile file = getFile(name);
		file.open();
		EntityInput input = new EntityInput(file);
		return input;
	}

	/* (non-Javadoc)
	 * @see org.apache.lucene.store.Directory#close()
	 */
	@Override
	public void close() throws IOException {
		//System.out.println("close...");
	}
	private EntityFile getFile(String name){
		if (fileMap.containsKey(name)){
			return fileMap.get(name);
		}else{
			EntityFile file = new EntityFile(name);
			if (!file.exists()){
				//System.out.println(file.getFilename());
				file.setDirectory(directory);
				file.save();
			}
			file.open();
			fileMap.put(name, file);
			return file;
		}
	}
}
